package com.sgb.kjwl.view.ui.fragment.machine;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ZoomControls;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.amap.api.maps.MapView;
import com.sgb.alimaplibrary.MapMarkerTools;
import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentMapLeaseBinding;
import com.sgb.kjwl.di.machine.DaggerMachineFragComponent;
import com.sgb.kjwl.di.machine.MachineFragModule;
import com.sgb.kjwl.model.entity.ParentAreaEntity;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.transport.MapCarsSourcesEntity;
import com.sgb.kjwl.view.widget.MapCarPopWindow;
import com.sgb.kjwl.view.widget.TwoAreaPopWindow;
import com.sgb.kjwl.view.widget.lease.LeaseAskMapPopWindow;
import com.sgb.kjwl.viewmodel.machine.MapLeaseFragModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.BasePopWindow;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

public class MapLeaseFragment extends AppFragment {

    private FragmentMapLeaseBinding binding;

    private MapCarPopWindow mapCarPopWindow;

    private List<PickSelectEntity> typeDatas = new ArrayList<>();
    private List<PickSelectEntity> classDatas = new ArrayList<>();
    private List<PickSelectEntity> sortDatas = new ArrayList<>();
    private List<ParentAreaEntity<List<ParentAreaEntity>>> cityDatas = new ArrayList<>();

    private String classNo = "";
    private String typeNo = "";
    private String sortNo = "";
    private List<String> addrNo = new ArrayList<>();

    private LeaseAskMapPopWindow classPopWindow;
    private LeaseAskMapPopWindow typePopWindow;
    private LeaseAskMapPopWindow sortPopWindow;
    private TwoAreaPopWindow areaPopWindow;

    @Inject
    MapLeaseFragModel mapLeaseFragModel;
    private MapView mMapView;
    private MapMarkerTools mapMarkerTools;
    List<MapCarsSourcesEntity> bundles = new ArrayList<>();

    public static MapLeaseFragment newInstance(String no,int position) {
        MapLeaseFragment fragment = new MapLeaseFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerMachineFragComponent.builder().appComponent(appComponent).machineFragModule(new MachineFragModule(this)).build().inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_map_lease,container,false);
        mapMarkerTools = new MapMarkerTools(getActivity(),getLifecycle());
        initMap(savedInstanceState);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
    }

    private void initView() {
        binding.classLayout.setOnClickListener(this);
        binding.typeLayout.setOnClickListener(this);
        binding.addrLayout.setOnClickListener(this);
        binding.sortLayout.setOnClickListener(this);

        classPopWindow = new LeaseAskMapPopWindow(getActivity());
        classPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object type, Object key2, Object key3) {
                classNo = ((PickSelectEntity)type).getProjectscale();
                binding.tvAddr.setText(((PickSelectEntity)type).getProjectscale());
//                binding.tvAddr.setTextColor(getResources().getColor(R.color.new_blue));
//                binding.addrLayoutRoot.setBackgroundResource(R.drawable.bg_pick_hall_selected);
//                binding.sortArrowView.setImageResource(R.mipmap.pick_select_down);
                getData();
            }
        });

        typePopWindow = new LeaseAskMapPopWindow(getActivity());
        typePopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object type, Object key2, Object key3) {
                typeNo = ((PickSelectEntity)type).getProjectscale();
                binding.tvAddr.setText(((PickSelectEntity)type).getProjectscale());
//                binding.tvAddr.setTextColor(getResources().getColor(R.color.new_blue));
//                binding.addrLayoutRoot.setBackgroundResource(R.drawable.bg_pick_hall_selected);
//                binding.sortArrowView.setImageResource(R.mipmap.pick_select_down);
                getData();
            }
        });

        areaPopWindow = new TwoAreaPopWindow(getActivity());
        areaPopWindow.setFocusable(false);
        areaPopWindow.setOutsideTouchable(false);
        areaPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                List<ParentAreaEntity> selectedEntities = (List<ParentAreaEntity>) key2;
                addrNo.clear();
                if (selectedEntities != null) {
                    for (ParentAreaEntity selected : selectedEntities) {
                        addrNo.add(selected.getNo());
                    }
                }
//                askLeaseFragModel.getList();
            }
        });
        areaPopWindow.setOnCloseBack(new TwoAreaPopWindow.OnCloseBack() {
            @Override
            public void dismiss() {

            }
        });

        sortPopWindow = new LeaseAskMapPopWindow(getActivity());
        sortPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object type, Object key2, Object key3) {
                sortNo = ((PickSelectEntity)type).getProjectscale();
                binding.tvAddr.setText(((PickSelectEntity)type).getProjectscale());
//                binding.tvAddr.setTextColor(getResources().getColor(R.color.new_blue));
//                binding.addrLayoutRoot.setBackgroundResource(R.drawable.bg_pick_hall_selected);
//                binding.sortArrowView.setImageResource(R.mipmap.pick_select_down);
                getData();
            }
        });
    }

    private void initData() {
        mapLeaseFragModel.getClassData();
        mapLeaseFragModel.getTypeData();
        mapLeaseFragModel.getCityData();
        mapLeaseFragModel.getSortData();
    }

    private void getData(){
        mapLeaseFragModel.getDataList();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.class_layout:
                if(null!=classDatas && classDatas.size()>0){
                    showClassPop();
                }
                break;
            case R.id.type_layout:
                if(null!=typeDatas && typeDatas.size()>0){
                    showTypePop();
                }
                break;
            case R.id.addr_layout:
                if(null!=cityDatas && cityDatas.size()>0){
                    showAddressPop();
                }
                break;
            case R.id.sort_layout:
                if(null!=sortDatas && sortDatas.size()>0){
                    showSortPop();
                }
                break;
        }
    }

    private void initMap(Bundle savedInstanceState) {
        mapCarPopWindow = new MapCarPopWindow(getActivity());
        mMapView = binding.mapView;
        if (mMapView.getChildCount() >= 2){
            View child = mMapView.getChildAt(1);
            if (child != null && (child instanceof ImageView || child instanceof ZoomControls)) {
                child.setVisibility(View.INVISIBLE);
            }
        }

        mapMarkerTools.init(savedInstanceState, mMapView);
        mapMarkerTools.setMarkerClick(new MapMarkerTools.MarkerClick() {
            @Override
            public void onMarkerClick(int pos,String title) {
                showMarkerView(pos,title);
            }

            @Override
            public void onLocationChanged(Double currentLat, Double currentLong, String address) {

            }
        });
    }

    public void showMarkerView(int pos,String title) {
        MapCarsSourcesEntity entity = bundles.get(pos);
        mapCarPopWindow.showPopwindowBottom(binding.mapView);
//        mapCarPopWindow.setData(entity);
    }

    private void addMarkInfoWindow(List<MapCarsSourcesEntity> datas) {
        try {
            mapMarkerTools.mapClear();
            bundles.clear();
            for (int i = 0; i < datas.size(); i++) {
                if (null != datas && datas.size() > 0) {
                    MapCarsSourcesEntity entity = datas.get(i);
                    if (null != entity && !TextUtils.isEmpty(entity.getLocation().get(1)) && !TextUtils.isEmpty(entity.getLocation().get(0))) {
                        double currentLat = Double.parseDouble(entity.getLocation().get(1));
                        double currentLong = Double.parseDouble(entity.getLocation().get(0));
                        if (currentLat == 0 || currentLong == 0) {
                            return;
                        }
                        mapMarkerTools.addPoiItem(currentLat, currentLong,"");
                        bundles.add(datas.get(i));
                    }
                }
            }
            mapMarkerTools.addAllMarkerToMap(R.mipmap.ic_map_overlay,false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showMapList(List<MapCarsSourcesEntity> datas) {
        addMarkInfoWindow(datas);
    }


    private void showClassPop(){

    }

    private void showTypePop(){

    }

    private void showAddressPop(){
        areaPopWindow.showPopwindow(binding.addrLayoutRoot);
    }

    private void showSortPop(){

    }

    public void showTypeDatas(List<PickSelectEntity> datas) {
        typeDatas.clear();
        PickSelectEntity entity = new PickSelectEntity();
        entity.setNo("0");
        entity.setProjectscale("全部");
        typeDatas.add(0,entity);
        typeDatas.addAll(datas);
    }

    public void showClassDatas(List<PickSelectEntity> list) {
        classDatas.clear();
        PickSelectEntity entity = new PickSelectEntity();
        entity.setNo("-1");
        entity.setProjectscale("全部");
        classDatas.addAll(list);
    }

    public void showAddressDatas(List<ParentAreaEntity<List<ParentAreaEntity>>> datas) {
        this.cityDatas = datas;
        areaPopWindow.setData(datas,false);
    }

    public void showSortDatas(List<PickSelectEntity> list) {
        sortDatas.clear();
        PickSelectEntity entity = new PickSelectEntity();
        entity.setNo("-1");
        entity.setProjectscale("全部");
        sortDatas.addAll(list);
    }
}
